  var websocket = null;
  //判断当前浏览器是否支持WebSocket
  if('WebSocket' in window){
  websocket = new WebSocket("ws://192.168.19.96:8080/haipumi_pro/customerServiceWebsocket");
  }
  else{
      alert('Not support websocket')
  }
  
  $(function(){
	  var uid=$("#lk_cool_effects_chatDiv_zindex").attr("uid");
	  if(uid!="" && uid!=undefined){
		  $("#lk_cool_effects_chatDiv_ID").attr("selfid","用户"+uid);
		  setTimeout('websocket.send("用户'+uid+',0-login")',5000);
	  }else{
		  $.ajax({
				type:"POST",
				url:"getRandomNum.action",
				dataType:"JSON",
				success: function(data){
					$("#lk_cool_effects_chatDiv_ID").attr("selfid","游客"+data.code);
					setTimeout('websocket.send("游客'+data.code+',0-login")',3000);
				}
			});
	  }
  })
  
  //连接发生错误的回调方法
  websocket.onerror = function(){
//      setMessageInnerHTML("error");
  };
   
  //连接成功建立的回调方法
  websocket.onopen = function(event){
//      setMessageInnerHTML("open");
  }
   
  //接收到消息的回调方法
  websocket.onmessage = function(e){
	  var a=e||window.event
      setMessageInnerHTML(a.data);
  }
   
  //连接关闭的回调方法
  websocket.onclose = function(){
//      setMessageInnerHTML("close");
  }
   
  //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
  window.onbeforeunload = function(){
      websocket.close();
  }
   
  //将消息显示在网页上
  function setMessageInnerHTML(innerHTML){
	  var data=jQuery.parseJSON(innerHTML)//将json字符串转为对象
	  var fromId=data.fromId;//发送人
	  if(fromId==1){//说明是服务器推送 的有新任务消息
		  var newTaskCount=parseInt( $("#newTaskCount").attr("count") );
		  var total=newTaskCount+1;
		  $("#newTaskCount").attr("count",total)
		  $("#newTaskCount").text(total+" 条新任务动态");
		  return;
	  }
	  var context=data.message;//消息
	  var time=DateDemo();
	  $(".lk_cool_effects_chatDiv_contents_right_message_chat").append('<div><div class="lk_cool_message_chat_customer">'+time+' '+fromId+' :</div><div class="lk_cool_message_chat_customer_message">'+context+'</div></div>');
	  moveScroll();
  }
   
  //关闭连接
  function closeWebSocket(){
      websocket.close();
  }
   
  //发送消息
  function sendChatInfo(obj){
	  if($(".lk_cool_effects_chatDiv_contents_right_message_none").css("display")!="none"){
		  $(".lk_cool_effects_chatDiv_contents_left_friendList li").first().click();//默认选择第一个客服
	  }
	  var context=$(obj).prev().val();
	  var time=DateDemo();
	  //发送人ID,接收人ID-MESSAGE
	  var message=$("#lk_cool_effects_chatDiv_ID").attr("selfid")+","+$("#lk_cool_effects_chatDiv_ID").attr("userid")+"-"+context;
	  $(".lk_cool_effects_chatDiv_contents_right_message_chat").append('<div><div class="lk_cool_message_chat_user">'+time+' 您 :</div><div class="lk_cool_message_chat_user_message">'+context+'</div></div>');
      websocket.send(message);
	  //$(obj).prev().val("");
	  moveScroll();
  }
  
  //获取当前时间
  function DateDemo(){   
	  var d, s = "";           				
	  d = new Date();      
	  s += d.getFullYear()+ "年";
	  s += (d.getMonth() + 1) + "月";            
	  s += d.getDate() + "日";                   
	  s += d.getHours() + ":";
	  s += d.getMinutes() + ":";
	  s += d.getSeconds();
	  return(s); 								
  }
  
  //向下滑动
  function moveScroll(){
	  var obj=$(".lk_cool_effects_chatDiv_contents_right_message_chat");
	  obj.animate({scrollTop:"999999"},"fast");//往下拉
  }